<!--  -->
<template>
  <div class="">
    <heads></heads>
    <main>
      <h1>管理员信息重新输入即可更新</h1>
      <div class="box">
        <div class="imageBox" style="position: relative">
          <el-image style="width: 140px; height: 140px; border-radius: 5px; position: absolute; top: 0px; left: 0; ckground-color: transparent" :src="img" fit="fill"></el-image>
          <el-upload action="http://127.0.0.1:8000/admin/Upload/addFile" list-type="picture-card" :limit="1" :on-success="updateImage" :auto-upload="true">
            <i slot="default" class="el-icon-plus"></i>
          </el-upload>
        </div>
        <div class="insertBox">
          <div class="inputBox">
            <label for="name">姓名</label>
            <input type="text" id="name" @blur="updateData" v-model="name" />
          </div>
          <div class="inputBox">
            <label for="address">地址</label>
            <input type="text" id="address" @blur="updateData" v-model="address" />
          </div>
          <div class="inputBox">
            <label for="info">个人简介</label>
            <input type="text" id="info" @blur="updateData" v-model="textInfo" />
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import heads from "@/components/adminHead.vue";

import { mapState } from "vuex";

import { selectIdUser, updateAdmin } from "@/api/getPostApi";

import { setStoageImage } from "@/config/Utils";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    heads,
  },
  data() {
    //这里存放数据
    return {
      name: "",
      img: "",
      textInfo: "",
      address: "",
      newPgth: "",
    };
  },
  //监听属性 类似于data概念
  computed: {
    ...mapState(["userId"]),
  },
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    updateData() {
      this.$message({
        message: "恭喜你，修改成功",
        type: "success",
      });

      updateAdmin(this.userId, this.name, this.address, this.textInfo);
    },
    updateImage(val) {
      setStoageImage(val);
      this.newPgth = val;
      this.img = `http://127.0.0.1:8000/${val}`;
      this.$message({
        message: "恭喜你，修改成功",
        type: "success",
      });
      updateAdmin(this.userId, this.name, this.address, this.textInfo, this.newPgth);
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    selectIdUser(this.userId).then((response) => {
      this.name = response.data.username;
      this.address = response.data.address;
      this.textInfo = response.data.textInfo;
      this.img = `http://127.0.0.1:8000/${response.data.userImg}`;
    });
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="scss" scoped>
//@import url(); 引入公共css类
main {
  width: 70%;
  margin: 1rem auto;
  .box {
    width: 100%;
    height: 500px;
    border-radius: 5px;
    background-color: #4e7ca1;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .insertBox {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      .inputBox {
        width: 35vw;
        display: flex;
        justify-content: space-around;
        align-items: center;
        color: white;
        label {
          font-size: 24px;
        }
        input {
          color: white;
          margin: 10px;
          width: 25vw;
          height: 30px;
          padding: 3px;
          background-color: transparent;
          border: none;
          outline: none;
          font-size: 20px;
        }
      }
    }
  }
  h1 {
    text-align: center;
  }
}
</style>
